<?php
//占
class class_cacheredis
{
	var $error = '';
	var $dir = '';
	var $prefix = 'cacheredis';
	//------------------------------------------------------------------------
	protected function _key($key)
	{
		return "{$this->prefix}_".md5($key);
	}
	//------------------------------------------------------------------------
	function has($key)
	{
		$k = $this->_key($key);
		return rdb_has($k);
	}
	//------------------------------------------------------------------------
	function delete($key)
	{
		if($this->has($key))
		{
			rdb_del($this->_key($key));
			return true;
		}
		
		return false;
	}
	//------------------------------------------------------------------------
	function deleteAll()
	{
		$keys = rdb_keys("{$this->prefix}_*");
		foreach($keys as $key)
		{
			rdb_del($key);
		}
	}
	//------------------------------------------------------------------------
	function get($key, $isarray = false)
	{
		if(!$this->has($key))
		{
			$this->error = 'isempty';
			return false;
		}
		
		$k = $this->_key($key);
		$v = rdb_get($k);

		if($v === '' || $v === false)
		{
			$this->error = 'emptyvalue';
			return false;
		}
				
		return $isarray ? unserialize($v) : $v;
	}
	//------------------------------------------------------------------------
	function set($key, $value, $time = 0, $isarray = false)
	{
		if($value === '')
			return true;
		
		$k = $this->_key($key);
		
		if($isarray)
			$value = serialize($value);
		
		return rdb_set($k, $value, $time);
	}
	//------------------------------------------------------------------------
	function geta($key)
	{
		return $this->get($key, 1);
	}
	//------------------------------------------------------------------------
	function seta($key, $value, $time = 0)
	{
		if(!is_array($value))
			return false;
		
		return $this->set($key, $value, $time, 1);
	}	
}